﻿'use strict';

// todoPage controller
app.controller('flowInfo', ['$state','$filter','$window','$http','$scope','$modal','toaster','$log','$timeout', function($state,$filter,$window,$http,$scope,$modal,toaster,$log,$timeout) {
    $scope.goods=[];//定义数组
    //删除一行
    $scope.dele =function($index){
        $scope.goods.splice($index,1);
    };
    //添加的方法
    $scope.addRows=function(){
        var id = new Date().getTime();
        var go = {
            "flag":false,
            "flowDetailsId":id,
            "flowDetailsName":"",
            "flowDetailsUp":"",
            "flowDetailsUpId":"",
            "flowDetailsDown":"",
            "flowDetailsDownId":"",
            "flowDetailsFzr":"",
            "flowDetailsFzrId":"",
            "flowDetailsNumber":"",
            "tiJiao":"",
            "baoCun":"",
            "tuiHui":""
        }
        $scope.goods.push(go);
    };

    //改变每行chekbox的状态
    $scope.ck = function($index){
        $scope.goods[$index].flag=!$scope.goods[$index].flag
    }
    //批量删除
    $scope.plsc = function(){
    //反着遍历
        for (var i = $scope.goods.length-1;i>=0;i--) {
            if ($scope.goods[i].flag) {
                $scope.goods.splice(i,1);
            }
        }
    }
    //全选
    var qq = true;
    $scope.qx = function(){
    //获取属性
        var ck = $("input[name=ck]")
        for (var i=0;i<ck.length;i++) {
            ck[i].checked=qq;
            //给每个数组中的ck赋值
            $scope.goods[i].flag=qq;
        }
        qq=!qq;
    };

    $scope.title = {
        title:'业务流程列表',
        addTitle:'业务流程信息'
    };
    $scope.toaster = {
        type: 'warning',
        title: '提示：',
        text: '内容不能为空！'
    };
    function addDo(){
        var notesdata = {};
        if(!$scope.row.flowInfoId){
            notesdata = {
                "flowInfoId":"",
                "flowInfoUse":$("#flowInfoUse").prop("checked"),
                "zdr":$scope.app.userName,
                "zdrId":$scope.app.userId,
                "useDb":$("#useDb").val(),
                "useDbUrl":$("#useDbUrl").val(),
                "details":$scope.goods
            }
        }else{
            notesdata = {
                "flowInfoId":$scope.row.flowInfoId,
                "flowInfoUse":$("#flowInfoUse").prop("checked"),
                "zdr":$scope.app.userName,
                "zdrId":$scope.app.userId,
                "useDb":$("#useDb").val(),
                "useDbUrl":$("#useDbUrl").val(),
                "details":$scope.goods
            }
        }
        var info = '';
        toaster.postInfo('/a_yongan/add/flowinfo',notesdata).then(function(data){
            if(data && data.data && data.data.success){
                showMessage('seccess','','保存成功！');
                $scope.warehouseAdd = false;
                $scope.warehouseList = true;
                getList();
                return info;
            }else{
                showMessage('warning','','操作失败！');
                info = "请求错误！";
            }
        });
    }
    function getList(){
        toaster.getInfo('/a_yongan/list/flowinfo',null).then(function(data){
            if(data && data.data && data.data.data){
                makeData(data.data.data);
            }
        });
    }
    getList();
    function makeData(data){
        data.forEach(function (item) {
            item.createTime = $filter("date")(item.createTime, "yyyy/MM/dd");
            if(item.flowInfoUse=='true'){
                item.flowInfoUse = true;
                item.status = '使用中...';
                item.status1 = 'label bg-info';
            }else{
                item.flowInfoUse = false;
                item.status = '禁用中...';
                item.status1 = 'label bg-danger';
            }
        })
        $scope.saveDate = data;
    }

    $scope.pop = function(){
        toaster.pop($scope.toaster.type, $scope.toaster.title, $scope.toaster.text);
    };
    function showMessage(type,title,text){
        $scope.toaster.type = type;
        $scope.toaster.text = text;
        toaster.pop($scope.toaster.type, $scope.toaster.title, $scope.toaster.text);
    }
    function checkItems(){
        var items = ['useDb','useDbUrl'];
        var itemsValue = ['使用模块','模块url'];
        for(var i=0;i<items.length;i++){
            if($('#'+items[i]+'').val()===''){
                $('#'+items[i]+'').focus();
                showMessage('warning','',itemsValue[i]+"不能为空！");
                return false;
            }
        }
        return true;
    }
    $scope.message = '是否确定保存？';

    $scope.saveInfo = function (size) {
        if(!checkItems()){
            return;
        }
        var modalInstance = $modal.open({
            templateUrl: 'myModalContent.html',
            controller: 'ModalInstanceCtrl',
            size: size,
            resolve: {
                items: function () {
                    return $scope.message;
                }
            }
        });
        modalInstance.result.then(function (selectedItem) {
            //var info = addDo();
            addDo();
            // if(addDo()){
            //     showMessage('seccess','','保存成功！');
            //     $scope.warehouseAdd = false;
            //     $scope.warehouseList = true;
            //     //$state.reload('app/porchasingOrder');
            // }else{
            //     showMessage('warning','',"保存失败！");
            //     return;
            // }

        }, function () {
            $log.info('Modal dismissed at: ' + new Date());
        });
    };
    $scope.isshow=true;
    $scope.warehouseAdd = false;
    $scope.warehouseList = true;
    $scope.warehouseJump = function(path) {
        $scope.row = {};
        $scope.goods = [];
        $scope.row = {
            'createTime':$filter("date")(new Date(), "yyyy/MM/dd"),
            'zdr':$scope.app.userName,
            'flowInfoUse':true
        };
        $scope.warehouseAdd = !($scope.warehouseAdd);
        $scope.warehouseList = !($scope.warehouseList);
    };
    $scope.row = {};
    $scope.columns = [
        {
            title:'创建时间',
            show:true
        },
        {
            title:'使用模块',
            show:true
        },
        {
            title:'模块url',
            show:true
        },
        {
            title:'状态',
            show:true
        }
    ];
    $scope.saveDate=[];
    $scope.showInfo = function(row){
        $scope.warehouseAdd = !($scope.warehouseAdd);
        $scope.warehouseList = !($scope.warehouseList);
        $scope.row = row;
        $scope.goods=row.details;
    };
    $scope.showSelectUser = function(index){
        var modalInstance = $modal.open({
            templateUrl: 'selectUser.html',
            controller: 'selectUserCtrl',
            size: 'sm',
            resolve: {
                items: function () {
                    return $scope.message;
                }
            }
        });
        modalInstance.result.then(function (selectedItem) {
            $scope.goods[index].flowDetailsFzr = selectedItem.name;
            $scope.goods[index].flowDetailsFzrId = selectedItem.id;
            showMessage('success','','选择成功！');
            //$scope.selected = selectedItem;
        }, function () {
            $log.info('Modal dismissed at: ' + new Date());
        });

    };
}])
;